//SPDX-FileCopyrightText: Copyright 2025-2025 深圳市同心圆网络有限公司
//SPDX-License-Identifier: GPL-3.0-only

use proto_gen_rust::board_api::board_api_client::BoardApiClient;
use proto_gen_rust::board_api::*;
use tauri::{AppHandle, Emitter, Runtime};

use crate::notice_decode::new_wrong_session_notice;

#[tauri::command]
pub async fn board_api_cmds_create<R: Runtime>(
    app_handle: AppHandle<R>,
    request: CreateRequest,
) -> Result<CreateResponse, String> {
    let chan = crate::get_grpc_chan(&app_handle).await;
    if (&chan).is_none() {
        return Err("no grpc conn".into());
    }
    let mut client = BoardApiClient::new(chan.unwrap());
    match client.create(request).await {
        Ok(response) => {
            let inner_resp = response.into_inner();
            if inner_resp.code == create_response::Code::WrongSession as i32 {
                if let Err(err) =
                    &app_handle.emit("notice", new_wrong_session_notice("create".into()))
                {
                    println!("{:?}", err);
                }
            }

            return Ok(inner_resp);
        }
        Err(status) => Err(status.message().into()),
    }
}

#[tauri::command]
pub async fn board_api_cmds_update_basic<R: Runtime>(
    app_handle: AppHandle<R>,
    request: UpdateBasicRequest,
) -> Result<UpdateBasicResponse, String> {
    let chan = crate::get_grpc_chan(&app_handle).await;
    if (&chan).is_none() {
        return Err("no grpc conn".into());
    }
    let mut client = BoardApiClient::new(chan.unwrap());
    match client.update_basic(request).await {
        Ok(response) => {
            let inner_resp = response.into_inner();
            if inner_resp.code == update_basic_response::Code::WrongSession as i32 {
                if let Err(err) =
                    &app_handle.emit("notice", new_wrong_session_notice("update_basic".into()))
                {
                    println!("{:?}", err);
                }
            }

            return Ok(inner_resp);
        }
        Err(status) => Err(status.message().into()),
    }
}

#[tauri::command]
pub async fn board_api_cmds_update_node_panel_setting<R: Runtime>(
    app_handle: AppHandle<R>,
    request: UpdateNodePanelSettingRequest,
) -> Result<UpdateNodePanelSettingResponse, String> {
    let chan = crate::get_grpc_chan(&app_handle).await;
    if (&chan).is_none() {
        return Err("no grpc conn".into());
    }
    let mut client = BoardApiClient::new(chan.unwrap());
    match client.update_node_panel_setting(request).await {
        Ok(response) => {
            let inner_resp = response.into_inner();
            if inner_resp.code == update_node_panel_setting_response::Code::WrongSession as i32 {
                if let Err(err) =
                    &app_handle.emit("notice", new_wrong_session_notice("update_node_panel_setting".into()))
                {
                    println!("{:?}", err);
                }
            }

            return Ok(inner_resp);
        }
        Err(status) => Err(status.message().into()),
    }
}

#[tauri::command]
pub async fn board_api_cmds_remove<R: Runtime>(
    app_handle: AppHandle<R>,
    request: RemoveRequest,
) -> Result<RemoveResponse, String> {
    let chan = crate::get_grpc_chan(&app_handle).await;
    if (&chan).is_none() {
        return Err("no grpc conn".into());
    }
    let mut client = BoardApiClient::new(chan.unwrap());
    match client.remove(request).await {
        Ok(response) => {
            let inner_resp = response.into_inner();
            if inner_resp.code == remove_response::Code::WrongSession as i32 {
                if let Err(err) =
                    &app_handle.emit("notice", new_wrong_session_notice("remove".into()))
                {
                    println!("{:?}", err);
                }
            }

            return Ok(inner_resp);
        }
        Err(status) => Err(status.message().into()),
    }
}

#[tauri::command]
pub async fn board_api_cmds_list_my<R: Runtime>(
    app_handle: AppHandle<R>,
    request: ListMyRequest,
) -> Result<ListMyResponse, String> {
    let chan = crate::get_grpc_chan(&app_handle).await;
    if (&chan).is_none() {
        return Err("no grpc conn".into());
    }
    let mut client = BoardApiClient::new(chan.unwrap());
    match client.list_my(request).await {
        Ok(response) => {
            let inner_resp = response.into_inner();
            if inner_resp.code == list_my_response::Code::WrongSession as i32 {
                if let Err(err) =
                    &app_handle.emit("notice", new_wrong_session_notice("list_my".into()))
                {
                    println!("{:?}", err);
                }
            }

            return Ok(inner_resp);
        }
        Err(status) => Err(status.message().into()),
    }
}

#[tauri::command]
pub async fn board_api_cmds_get<R: Runtime>(
    app_handle: AppHandle<R>,
    request: GetRequest,
) -> Result<GetResponse, String> {
    let chan = crate::get_grpc_chan(&app_handle).await;
    if (&chan).is_none() {
        return Err("no grpc conn".into());
    }
    let mut client = BoardApiClient::new(chan.unwrap());
    match client.get(request).await {
        Ok(response) => {
            let inner_resp = response.into_inner();
            if inner_resp.code == get_response::Code::WrongSession as i32 {
                if let Err(err) =
                    &app_handle.emit("notice", new_wrong_session_notice("get".into()))
                {
                    println!("{:?}", err);
                }
            }

            return Ok(inner_resp);
        }
        Err(status) => Err(status.message().into()),
    }
}

#[tauri::command]
pub async fn board_api_cmds_add_llm<R: Runtime>(
    app_handle: AppHandle<R>,
    request: AddLlmRequest,
) -> Result<AddLlmResponse, String> {
    let chan = crate::get_grpc_chan(&app_handle).await;
    if (&chan).is_none() {
        return Err("no grpc conn".into());
    }
    let mut client = BoardApiClient::new(chan.unwrap());
    match client.add_llm(request).await {
        Ok(response) => {
            let inner_resp = response.into_inner();
            if inner_resp.code == add_llm_response::Code::WrongSession as i32 {
                if let Err(err) =
                    &app_handle.emit("notice", new_wrong_session_notice("add_llm".into()))
                {
                    println!("{:?}", err);
                }
            }

            return Ok(inner_resp);
        }
        Err(status) => Err(status.message().into()),
    }
}

#[tauri::command]
pub async fn board_api_cmds_update_llm<R: Runtime>(
    app_handle: AppHandle<R>,
    request: UpdateLlmRequest,
) -> Result<UpdateLlmResponse, String> {
    let chan = crate::get_grpc_chan(&app_handle).await;
    if (&chan).is_none() {
        return Err("no grpc conn".into());
    }
    let mut client = BoardApiClient::new(chan.unwrap());
    match client.update_llm(request).await {
        Ok(response) => {
            let inner_resp = response.into_inner();
            if inner_resp.code == update_llm_response::Code::WrongSession as i32 {
                if let Err(err) =
                    &app_handle.emit("notice", new_wrong_session_notice("update_llm".into()))
                {
                    println!("{:?}", err);
                }
            }

            return Ok(inner_resp);
        }
        Err(status) => Err(status.message().into()),
    }
}

#[tauri::command]
pub async fn board_api_cmds_remove_llm<R: Runtime>(
    app_handle: AppHandle<R>,
    request: RemoveLlmRequest,
) -> Result<RemoveLlmResponse, String> {
    let chan = crate::get_grpc_chan(&app_handle).await;
    if (&chan).is_none() {
        return Err("no grpc conn".into());
    }
    let mut client = BoardApiClient::new(chan.unwrap());
    match client.remove_llm(request).await {
        Ok(response) => {
            let inner_resp = response.into_inner();
            if inner_resp.code == remove_llm_response::Code::WrongSession as i32 {
                if let Err(err) =
                    &app_handle.emit("notice", new_wrong_session_notice("remove_llm".into()))
                {
                    println!("{:?}", err);
                }
            }

            return Ok(inner_resp);
        }
        Err(status) => Err(status.message().into()),
    }
}